﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChequeManagement.PL.ChequeCategory
{
    public partial class ChequeCategory : PL.Base.Base
    {
        public ChequeCategory()
        {
            InitializeComponent();
        }

        private void ChequeCategory_Load(object sender, EventArgs e)
        {

            // TODO: This line of code loads data into the 'cMDBDataSet.ChequeCategory' table. You can move, or remove it, as needed.
           ChequeCategorydataGridView.DataSource = readFromDataBase();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            BLL.BLLChequeCategoryFactory cf = new BLL.BLLChequeCategoryFactory();

            try
            {
                BLL.BLLChequeCategory cc = new BLL.BLLChequeCategory();
                BLL.BLLBank objBank = new BLL.BLLBank();
                objBank.BankID = 1;
                
                cc.ChequeCategoryName = ChequeCategroyNametxtBox.Text;
                cc.BankObject = objBank;
               
                cc.BranchName = BranchNametxtBox.Text;
                cc.AccountNo = Convert.ToInt64(AccountNotxtBox.Text);
                cc.PageCount = Convert.ToInt32(PageCounttxtBox.Text);
                cc.FirstPageNo = Convert.ToInt64(FirstPagetxtBox.Text);

                cf.BeginTransaction();
                cf.Add(cc);
                cf.CommitTransaction();
                ChequeCategorydataGridView.DataSource = readFromDataBase();

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        private DataTable readFromDataBase()
        {
            DataTable dt = new DataTable();
            BLL.BLLChequeCategoryFactory cf = new BLL.BLLChequeCategoryFactory();
            cf.BeginTransaction();
            cf.GetAll(dt);
            cf.CommitTransaction();
            return dt;
        }

        private void deleteChequeCategory(int id)
        {
            BLL.BLLChequeCategoryFactory cf = new BLL.BLLChequeCategoryFactory();
            cf.BeginTransaction();
            cf.Remove(id);
            cf.CommitTransaction();

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                deleteChequeCategory((int)ChequeCategorydataGridView.CurrentRow.Cells[0].Value);

                ChequeCategorydataGridView.DataSource = readFromDataBase();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            BLL.BLLChequeCategoryFactory cf = new BLL.BLLChequeCategoryFactory();
            BLL.BLLChequeCategory cc = new BLL.BLLChequeCategory();
            BLL.BLLBank objBank = new BLL.BLLBank();
            objBank.BankID = 1;
            cc.BankObject = objBank;
            cc.ChequeCategoryID = Convert.ToInt32(this.ChequeCategorydataGridView.CurrentRow.Cells["ChequeCategoryID"].Value);
            cc.ChequeCategoryName = ChequeCategroyNametxtBox.Text;
            cc.BranchName = BranchNametxtBox.Text;
            cc.AccountNo = Convert.ToInt64(AccountNotxtBox.Text);
            cc.PageCount = Convert.ToInt32(PageCounttxtBox.Text);
            cc.FirstPageNo = Convert.ToInt64(FirstPagetxtBox.Text);

            cf.BeginTransaction();
            cf.Edit(cc, (int)cc.ChequeCategoryID);
            cf.CommitTransaction();

            int rowIndex = this.ChequeCategorydataGridView.CurrentRow.Index;
            ChequeCategorydataGridView.DataSource = readFromDataBase();
            //Restore the last index
            this.ChequeCategorydataGridView.CurrentCell = this.ChequeCategorydataGridView[0, rowIndex];
            
        }

        
    }
}
